package com.example.mvc;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.User;
import com.example.service.UserService;
import com.example.util.Md5util;

@Controller
@RequestMapping("/getUsers" )
@SessionAttributes(value={"uname","pwd"})
public class UserMvc {

	@Autowired
	private UserService userService;
	
	
	private List<User> list;
	
	private String pwd;
	
	private int aw;
	
	private User user;
	
	private ModelAndView modelAndView;
	
	
	private String uuid;
	
	@RequestMapping("/addUser")
	public String addUser(@ModelAttribute("user")User user){
	    pwd=user.getPwd();
	    uuid=UUID.randomUUID().toString();
	    pwd=Md5util.Md5(pwd);
	    user.setUuid(uuid);
	    
	    user.setPwd(pwd);
	    aw=userService.add(user);
	    return Asd(aw,"/login","/error");
	}
			
	@RequestMapping("/deleteUser/{id}")
	public String deleteUser(@PathVariable("id")int id){
		aw=userService.delete(id);
		return Asd(aw,"/selectUsers","/error");
	}
	
	@RequestMapping("/updateUser")
	public String updateUser(@ModelAttribute("user")User user){
		aw=userService.update(user);
		return Asd(aw,"/updateUser","/error");
	}
	
	
	
	
	public String Asd(int aw,String args1,String args2){
		if(aw==1){
			return args1;
		}else{
			return args2;
		}
		
	}
	
	/**
	 * 连接页面的方法
	 * 
	 */
	
	@RequestMapping("/")
	public String index(){
		
		return "/index";
	}
	
	@RequestMapping("/addUser1")
	public String addUser1(){
		
		return "/addUser";
	}
	

	
}
